package com.mingmen.supplier.activity;


import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.google.gson.Gson;
import com.lzy.imagepicker.ImagePicker;
import com.mingmen.supplier.R;
import com.mingmen.supplier.adapter.DingDanZongListAdapter;
import com.mingmen.supplier.adapter.NewWeiHuListAdapter;
import com.mingmen.supplier.http.callback.StringCallback;
import com.mingmen.supplier.httpconfig.HttpResponse;
import com.mingmen.supplier.immersionbar.ImmersionBar;
import com.mingmen.supplier.model.DingDanZongModel;
import com.mingmen.supplier.model.WeiHuModel;
import com.mingmen.supplier.util.GsonUtil;
import com.mingmen.supplier.util.SharePreferenceUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

public class DingDanZongListActivity extends AppCompatActivity implements DingDanZongListAdapter.OnItemClickListener {

    //返回按钮
    @BindView(R.id.text_return)
    TextView textReturn;

    private int resurtResurant = 100;
    @BindView(R.id.recycle_shopo)
    RecyclerView mRecyclerView;

    DingDanZongListAdapter commodityAdapter;

    List<DingDanZongModel.DataBean> dataList = new ArrayList<>();

    LinearLayoutManager linearLayoutManager;


    @BindView(R.id.view_dishline)
    View view_dishline;
    @BindView(R.id.linear_return)
    LinearLayout linear_return;
    String token;
    String jdataData;
    String date_Send;
    TimePickerView pvTime;
    @BindView(R.id.layout_date)
    LinearLayout layout_date;
    @BindView(R.id.textview_calendafirst)
    TextView textview_calendafirst;
    String sumDate;
    @BindView(R.id.layout_list)
    RelativeLayout layout_list;
    @BindView(R.id.layout_nodata)
    LinearLayout layout_nodata;
    @BindView(R.id.img_jingying)
    ImageView img_jingying;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dingdanzong);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
                .statusBarDarkFont(true, 0.2f)
                .init();
        linearLayoutManager = new LinearLayoutManager(this);
        getCaneda();
        token = (String) SharePreferenceUtil.get(this, "token", "");
//        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4);
        jdataData = getIntent().getStringExtra("jdataData");
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(System.currentTimeMillis());
        System.out.println(formatter.format(date));
        date_Send = formatter.format(date);
        sumDate = date_Send;
        mRecyclerView.setLayoutManager(linearLayoutManager);
        commodityAdapter = new DingDanZongListAdapter(this, dataList);
        mRecyclerView.setAdapter(commodityAdapter);

        commodityAdapter.setOnItemClickListener(this);
        getData(token, sumDate);

    }

    public void getData(String token, String sumDate) {
        HttpResponse.sumOrderDaily(token, sumDate, new StringCallback(this) {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                Log.d("WeihuListActivity", s);
                if (dataList.size() > 0) {
                    dataList.clear();
                }


                DingDanZongModel weiHuModel = new DingDanZongModel();
                Gson gson = new Gson();
                weiHuModel = gson.fromJson(s, DingDanZongModel.class);

//                weiHuModel = GsonUtil.GsonToBean(s, DingDanZongModel.class);
//                dataList.addAll(weiHuModel.getData());
                dataList.addAll(weiHuModel.getData());
                if (dataList.size() == 0) {
                    layout_list.setVisibility(View.GONE);
                    layout_nodata.setVisibility(View.VISIBLE);
                } else {
                    layout_list.setVisibility(View.VISIBLE);
                    layout_nodata.setVisibility(View.GONE);

                }
                commodityAdapter.setDataList(dataList);
            }
        });
    }

    @OnClick({R.id.text_return, R.id.layout_date, R.id.textview_calendafirst, R.id.linear_return})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.text_return:
            case R.id.linear_return:
                finish();
                break;
            case R.id.layout_date:
            case R.id.textview_calendafirst:

                img_jingying.setRotation(360);
                img_jingying.setImageResource(R.mipmap.arrowblue_bottom);

                pvTime.show(layout_date);
                break;
        }
    }

    @Override
    public void onItemClickListener(View view, int pos, List<DingDanZongModel.DataBean> myLiveList) {
        switch (view.getId()) {
            case R.id.text_manager:
//                startActivity(new Intent(DingDanZongListActivity.this, CommodityShopManagerActivity.class));
                break;
            default:
//                startActivity(new Intent(DingDanZongListActivity.this, NoticeDetailActivity.class));
        }
    }


    public void getCaneda() {
        Calendar selectedDate = Calendar.getInstance();//系统当前时间
        Calendar startDate = Calendar.getInstance();
        startDate.set(2020, 1, 1);
        Calendar endDate = Calendar.getInstance();
        int y = endDate.get(Calendar.YEAR);
        int m = endDate.get(Calendar.MONTH);
        int d = endDate.get(Calendar.DATE);
        endDate.set(y, m, d);
        pvTime = new TimePickerBuilder(DingDanZongListActivity.this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                textview_calendafirst.setText(getTime(date));
                sumDate = getTime(date);

                getData(token, sumDate);

            }
        }).setDate(selectedDate)
                .setRangDate(startDate, endDate)
                .setLayoutRes(R.layout.pickerview_custom_lunar, new CustomListener() {

                    @Override
                    public void customLayout(final View v) {
                        final TextView tvSubmit = (TextView) v.findViewById(R.id.tv_finish);
                        ImageView ivCancel = (ImageView) v.findViewById(R.id.iv_cancel);
                        tvSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvTime.returnData();
                                img_jingying.setImageResource(R.mipmap.arrowblue_bottom);

                                img_jingying.setRotation(180);

                                pvTime.dismiss();
                            }
                        });
                        ivCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                img_jingying.setImageResource(R.mipmap.arrowblue_bottom);

                                img_jingying.setRotation(180);
                                pvTime.dismiss();
                            }
                        });

                    }


                })
                .setType(new boolean[]{true, true, true, false, false, false})
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setDividerColor(getResources().getColor(R.color.canedacolor))
                .setOutSideCancelable(false)
                .build();
    }

    private String getTime(Date date) {//可根据需要自行截取数据显示
        Log.d("getTime()", "choice date millis: " + date.getTime());
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }
}




